<template>
  <a-modal v-model="visible" :width="1080" :footer="null" title="推荐商品">

    <!-- 查询区域-END -->
    <div class="table-operator">
      <a-button @click="goodSelect" type="primary" icon="plus">添加商品</a-button>
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table ref="table" size="middle" bordered rowKey="id" :columns="columns" :dataSource="dataSource" :pagination="{ pageSize: 10 }"
        :scroll="{ y: 400 }" class="j-table-force-nowrap">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="text" height="55px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDeleteSelf(record.recommendedMapId)">删除</a>
        </span>

      </a-table>
    </div>
    <gms-good-ref ref="modalGoodsSet" :selectionType="'checkbox'" @ok="modalGoodsOk" />

  </a-modal>
</template>

<script>
import { postAction, getAction, deleteAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import GmsGoodRef from '@/views/gospel/mallBase/good/modules/GmsGoodRef'


export default {
  name: 'GmsGoodRecommendedMapList',
  mixins: [mixinDevice],
  components: {
    GmsGoodRef
  },
  data () {
    return {
      id: null,
      dataSource: [],
      visible: false,
      description: '推荐商品管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '商品id',
          align: "center",
          width: '25%',
          dataIndex: 'id'
        },
        {
          title: '商品图片',
          align: "center",
          dataIndex: 'imgUrl',
          scopedSlots: { customRender: "imgSlot" }
        },
        {
          title: '名称',
          align: "center",
          dataIndex: 'title',
          width: '40%',
          ellipsis: true,
        },
        {
          title: '货号',
          align: "center",
          dataIndex: 'code'
        },

        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/mallBase/gmsGoodRecommendedMap/list",
        delete: "/mallBase/gmsGoodRecommendedMap/delete",
        deleteBatch: "/mallBase/gmsGoodRecommendedMap/deleteBatch",
        exportXlsUrl: "/mallBase/gmsGoodRecommendedMap/exportXls",
        importExcelUrl: "mallBase/gmsGoodRecommendedMap/importExcel",

      },
      dictOptions: {},
    }
  },
  created () {
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    open (id) {
      let self = this
      self.id = id
      getAction("/mallBase/gmsGood/queryRecommendById", { id: id }).then(res => {
        if (res.success) {
          if (res.result) {
            this.dataSource = res.result
          } else {
            this.dataSource = []
          }

        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleDeleteSelf: function (id) {
      if (!this.url.delete) {
        this.$message.error("请设置url.delete属性!")
        return
      }
      var that = this
      deleteAction(that.url.delete, { id: id }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.open(that.id)
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    modalGoodsOk (rows, keys) {
      const self = this
      let itemIds = []
      for (let i = 0; i < rows.length; i++) {
        if (self.id != rows[i].id) {
          itemIds.push(rows[i].id)
        }
      }
      postAction("/mallBase/gmsGood/setRecommended", { itemId: self.id, itemIds: itemIds }).then(res => {
        if (res.success) {
          self.open(self.id)
        } else {
          this.$message.error(res.message)
        }
      })



    },
    goodSelect () {
      let self = this
      self.$refs.modalGoodsSet.visible = true
      self.$refs.modalGoodsSet.checkedKeys = []
      self.$refs.modalGoodsSet.checkedRows = this.dataSource
      for (let i = 0; i < this.dataSource.length; i++) {
        self.$refs.modalGoodsSet.checkedKeys.push(this.dataSource[i].itemCode)
      }
      self.$refs.modalGoodsSet.open()
    },
  }
}
        </script>
<style scoped>
@import '~@assets/less/common.less';
</style>
